public member function
<regex>

std::match_results::operator[]

const_reference operator[] (size_type n) const;
Return match
Returns the n-th match in a match_results object that is ready.

The reference returned is of the appropriate sub_match type and can be used to access directly its members.

The match_results object shall be ready, which happens after it has been passed as the proper argument in a call to either regex_match or regex_search.

Parameters

n
Match number. This shall be lower than match_results::size.
The match number 0 represents the entire matched expression. Subsequent match numbers identify the sub-expressions, if any.
Member type size_type is an unsigned integral type.

Return value

A reference to the sub_match object describing the n-th match.

Member type const_reference is an alias of the reference to the appropriate const sub_match type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// match_results::operator[]
// - using cmatch, a standard alias of match_results<const char*>
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::cmatch m;

  std::regex_match ( "subject", m, std::regex("(sub)(.*)") );

  for (unsigned i=0; i<m.size(); ++i) {
    std::cout << "match " << i << ": " << m[i];
    std::cout << " (with a length of " << m[i].length() << ")\n";
  }

  return 0;
}

Output:
match 0: subject (with a length of 7)
match 1: sub (with a length of 3)
match 2: ject (with a length of 4)



See also